<%@ WebHandler Language="C#" Class="upload_do" %>

using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Web;
using System.Web.Services;
using System.Globalization;


public class upload_do : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        //检验上传权限
        String savePath = "/UpImg/";
        String dirPath = HttpContext.Current.Server.MapPath(savePath);
        //创建文件夹
        String ymd = DateTime.Now.ToString("yyyy-MM", DateTimeFormatInfo.InvariantInfo);

        dirPath += ymd + "/";
        string imgurl = savePath + ymd + "/";
        if (!Directory.Exists(dirPath))
        {
            Directory.CreateDirectory(dirPath);
        }
        string newname = DateTime.Now.ToString("yyyyMMddHHmmss", DateTimeFormatInfo.InvariantInfo) + ".jpg";
        dirPath += newname;
        imgurl += newname;
        //try
        //{
        string img = context.Request.Params["data"];
        
        img = img.Substring(img.IndexOf("data:") + 5);
        //context.Response.Write(img);
        if (Base64StringToImage(img, dirPath))
        {
            context.Response.Write(imgurl);
        }
        else
        {
            context.Response.Write("上传错误!");
        }
        context.Response.End();
    }

    private byte[] getByte(HttpPostedFile file)
    {
        byte[] buffer = new byte[file.ContentLength];
        file.InputStream.Read(buffer, 0, buffer.Length);
        return buffer;
    }


    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    //图片转为base64编码的字符串
    protected string ImgToBase64String(string Imagefilename)
    {
        try
        {
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(Imagefilename);

            MemoryStream ms = new MemoryStream();
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
            byte[] arr = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(arr, 0, (int)ms.Length);
            ms.Close();
            return Convert.ToBase64String(arr);
        }
        catch (Exception ex)
        {
            return null;
        }
    }

    //base64编码的字符串转为图片
    protected bool Base64StringToImage(string strbase64, string txtFileName)
    {
        //try
        //{
        byte[] arr = Convert.FromBase64String(strbase64);
        MemoryStream ms = new MemoryStream(arr);
        System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ms);
        bmp.Save(txtFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
        //bmp.Save(txtFileName + ".bmp", ImageFormat.Bmp);  
        //bmp.Save(txtFileName + ".gif", ImageFormat.Gif);  
        //bmp.Save(txtFileName + ".png", ImageFormat.Png); 
        ms.Close();
        return true;
        //    }
        //        catch (Exception ex)
        //        {
        //            return null;
        //}
    }
}
